#coding:utf-8
import os
from common.koufuApi import KoufuApi
from common.koufuShopApi import KoufuShopApi

__author__ = 'yangxianming'

from common.KoufuConf import KoufuConf
import json
import requests


def case():
    #初始化必要的参数
    kou_fu_conf = KoufuConf()
    test_url = kou_fu_conf.shop_domain
    kou_fu_shop_api = KoufuShopApi()
    script_name = os.path.basename(__file__)
    tf = True
    test_result = ""

    try:
        #授权获得token
        p = kou_fu_shop_api.get_api_auth_koufu_authorize_data(update_body={"thirdUserId":"25244122401072542"})
        r = requests.post(test_url+kou_fu_shop_api.get_api_auth_koufu_authorize_data(get_path=1), data=json.dumps(p), headers=kou_fu_conf.shop_headers)
        print("这个是接口api_auth_koufu_authorize返回的全部参数\n", r.content)
        token = r.json()["return_data"]["token"]
        kou_fu_conf.shop_headers.update({"X-Efulishop-Token":token})

        #拿到付款前福豆余额
        p = kou_fu_shop_api.get_api_pay_payment_data()
        r = requests.post(test_url+kou_fu_shop_api.get_api_pay_payment_data(get_path=1), data=json.dumps(p), headers=kou_fu_conf.shop_headers)
        print("这个是接口api_pay_payment返回的全部参数\n", r.content)
        before_balance = r.json()["return_data"]["balance"]

        #商城下单  421(秒杀测试商品) 250(自动化测试商品)
        p = kou_fu_shop_api.get_api_order_atOnceSubmit_data(update_body={"productId":"421"})
        r = requests.post(test_url+kou_fu_shop_api.get_api_order_atOnceSubmit_data(get_path=1), data=json.dumps(p), headers=kou_fu_conf.shop_headers)
        print("这个是接口api_order_atOnceSubmit返回的全部参数\n", r.content.decode())
        order_sn = r.json()["return_data"]["orderSn"]

        #聚合支付
        p = kou_fu_shop_api.get_api_pay_smartPay_data(update_body={"orderSn":order_sn})
        r = requests.post(test_url+kou_fu_shop_api.get_api_pay_smartPay_data(get_path=1), data=json.dumps(p), headers=kou_fu_conf.shop_headers)
        print("这个是接口api_pay_smartPay返回的全部参数\n", r.content)

        #拿到付款前福豆余额
        p = kou_fu_shop_api.get_api_pay_payment_data()
        r = requests.post(test_url+kou_fu_shop_api.get_api_pay_payment_data(get_path=1), data=json.dumps(p), headers=kou_fu_conf.shop_headers)
        print("这个是接口api_pay_payment返回的全部参数\n", r.content)
        after_balance = r.json()["return_data"]["balance"]

        #订单详情
        p = kou_fu_shop_api.get_api_order_orderDetail_data(update_body={"orderSn":order_sn})
        r = requests.post(test_url+kou_fu_shop_api.get_api_order_orderDetail_data(get_path=1), data=json.dumps(p), headers=kou_fu_conf.shop_headers)
        print("这个是接口api_order_orderDetail返回的全部参数\n", r.content)

        balance_different = int((before_balance - after_balance) * 100)
        print( balance_different)

        test_result = r.json()
        voucherId = test_result["return_data"]["voucherId"]
        print("返回参数voucherId是否是空", voucherId)
        #具体判断
        if voucherId is not None:
            tf = False
    except Exception as e:
        err_info = str(e).replace("'", "").replace("\"", "")
        test_result = "非预期的异常,脚本文件:" + script_name + "; 错误信息：" + err_info
        tf = -2

    #定义预期描述
    print(str(tf))
    print(test_result)
    expect_result = "返回参数ebuyCode是否是空"

    #返回测试结果 第一个参数:True or False    第二个参数测试结果描述
    return tf, [test_result, expect_result]

case()